# -*- coding: utf-8 -*-
__author__ = 'dalon'

from define import *
from api import deviceControl
from api import interface_config_handle
from api import add_ip_addr
import time
import os, sys
try:
    os.remove('.\log\fw_1')
    os.remove('.\log\ADC')
    os.remove('.\log\Switch')
except:
    pass

dut_dc = deviceControl(dut['type'], dut['hostname'], dut['mgmt'])
router1_dc = deviceControl(router1['type'], router1['hostname'], router1['mgmt'], username = 'cisco', password ='cisco')
router2_dc = deviceControl(router2['type'], router2['hostname'], router2['mgmt'])
dut['controller'] = dut_dc
router1['controller'] = router1_dc
router2['controller'] = router2_dc

#section 1: bridge forwarding coverage test
dut_dc.create_vlan(vlan_bridge)
router1_dc.create_vlan(vlan_bridge)
router2_dc.create_vlan(vlan_bridge)

while 1:
     
    for left_interface in if_type_bridge:
        for right_interface in if_type_bridge:
            print 'left interface: %s, right interface: %s'%(left_interface, right_interface)
            #左侧接口配置
            interface_config_handle(left_interface,
                                    dut_dc,
                                    dut_connect_desc[dut['hostname']]['left_interface'].split('-')[0],
                                    vlan_bridge, 
                                    trunk_id_left)
            interface_config_handle(left_interface,
                                    router1_dc,
                                    dut_connect_desc[dut['hostname']]['left_interface'].split('-')[2],
                                    vlan_bridge, 
                                    trunk_id_left)
            #右侧接口配置
            interface_config_handle(right_interface,
                                    dut_dc,
                                    dut_connect_desc[dut['hostname']]['right_interface'].split('-')[0],
                                    vlan_bridge, 
                                    trunk_id_right)
            interface_config_handle(right_interface,
                                    router2_dc,
                                    dut_connect_desc[dut['hostname']]['right_interface'].split('-')[2],
                                    vlan_bridge, 
                                    trunk_id_right)
            print "ok"
            #sys.exit()
            time.sleep(100)
                
    
    #section 2: router forwarding coverage test
    for left_interface in if_type_router:
        for right_interface in if_type_router:
            print 'left interface: %s, right interface: %s'%(left_interface, right_interface)
            #左侧接口配置
            interface_config_handle(left_interface,
                                    dut_dc,
                                    dut_connect_desc[dut['hostname']]['left_interface'].split('-')[0],
                                    vlan_left, 
                                    trunk_id_left)
            add_ip_addr(left_interface, 
                        dut_dc, 
                        dut_connect_desc[dut['hostname']]['left_interface'].split('-')[0], 
                        vlan_left_dut_ip, mask,
                        vlan_left_dut_ip6, prefix, 
                        vlan_left, 
                        trunk_id_left)
            interface_config_handle(left_interface,
                                    router1_dc,
                                    dut_connect_desc[dut['hostname']]['left_interface'].split('-')[2],
                                    vlan_left, 
                                    trunk_id_left)
            add_ip_addr(left_interface, 
                        router1_dc, 
                        dut_connect_desc[dut['hostname']]['left_interface'].split('-')[2], 
                        vlan_left_router1_ip, mask,
                        vlan_left_router1_ip6, prefix,
                        vlan_left, 
                        trunk_id_left)
            #右侧接口配置
            interface_config_handle(right_interface,
                                    dut_dc,
                                    dut_connect_desc[dut['hostname']]['right_interface'].split('-')[0],
                                    vlan_right, 
                                    trunk_id_right)
            add_ip_addr(right_interface, 
                        dut_dc, 
                        dut_connect_desc[dut['hostname']]['right_interface'].split('-')[0], 
                        vlan_right_dut_ip, mask,
                        vlan_right_dut_ip6, prefix, 
                        vlan_right, 
                        trunk_id_right)
            interface_config_handle(right_interface,
                                    router2_dc,
                                    dut_connect_desc[dut['hostname']]['right_interface'].split('-')[2],
                                    vlan_right, 
                                    trunk_id_right)
            add_ip_addr(right_interface, 
                        router2_dc, 
                        dut_connect_desc[dut['hostname']]['right_interface'].split('-')[2], 
                        vlan_right_router2_ip, mask,
                        vlan_right_router2_ip6, prefix,
                        vlan_right, 
                        trunk_id_right)
            
            time.sleep(100)
    
            dut_dc.default_interface_config('vlan%s'%vlan_left)
            dut_dc.default_interface_config('vlan%s'%vlan_right)
            router1_dc.default_interface_config('vlan%s'%vlan_left)
            router2_dc.default_interface_config('vlan%s'%vlan_right)
    
    print '####################################################################\n'*10
    
#
dut_dc.exit()
router1_dc.exit()
router2_dc.exit()